package com.bkgy.modular.tbdsjrawcheckc642.service.impl;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.TemplateExportParams;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bkgy.excelhandle.PlanExcelHandlerImpl;
import com.bkgy.modular.calsidata.entity.TbFalt;
import com.bkgy.modular.qms.entity.QmsCurveDataMongo;
import com.bkgy.modular.tbdsjrawcheckc642.dao.*;
import com.bkgy.modular.tbdsjrawcheckc642.entity.*;
import com.bkgy.modular.tbdsjrawcheckc642.service.TbDsjRawcheckC642Service;
import com.bkgy.sys.common.entity.ResultData;
import com.bkgy.util.CalUtils;
import com.bkgy.util.ExcelUtilsPOI;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.util.ResourceUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.sql.Timestamp;
import java.util.*;


@Service
@Slf4j
public class TbDsjRawcheckC642ServiceImpl extends ServiceImpl<TbDsjRawcheckC642Mapper, TbDsjRawcheckC642> implements TbDsjRawcheckC642Service {

    @Resource
    private MongoTemplate mongoTemplate;

    @Resource
    private CalUtils calUtils;

    @Resource
    private RestTemplate restTemplate;

    @Resource
    private TbDsjRawcheckC642Mapper tbDsjRawcheckC642Mapper;

    @Resource
    private TbDsjActWeightMapper tbDsjActWeightMapper;

    @Resource
    private ComparisonExtidC621Mapper comparisonExtidC621Mapper;

    @Resource
    private ComparisonExtidC622Mapper comparisonExtidC622Mapper;

    @Resource
    private ComparisonExtidC642Mapper comparisonExtidC642Mapper;

    @Resource
    private TbDsjActYieldMapper tbDsjActYieldMapper;

    @Autowired
    private TbDsjRecalculateThkToleC622Mapper tbDsjRecalculateThkToleC622mapper;

    @Override
    public ResultData findDataByPage(JSONObject json) {
        try {
            Integer pageIndex = json.getInteger("pageIndex");
            Integer pageSize = json.getInteger("pageSize");
            if (pageIndex == null || pageSize == null) {
                return ResultData.ResultDataFaultSelf("分页参数没有传", null);
            }
            //查询条件
            JSONObject condition = json.getJSONObject("condition");
            List<TbDsjRawcheckC642> list = tbDsjRawcheckC642Mapper.selectByPageList((pageIndex - 1) * pageSize,pageSize * pageIndex,condition);
            Integer dataCount = tbDsjRawcheckC642Mapper.findDataCount(condition);
            return ResultData.ResultDataSuccess(list,dataCount);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultData.ResultDataFaultSelf("查询失败,失败信息为" + e.getMessage(), null);
        }
    }


    @Override
    public ResultData findDataByPage2(JSONObject json) {
        try {
            Integer pageIndex = json.getInteger("pageIndex");
            Integer pageSize = json.getInteger("pageSize");
            if (pageIndex == null || pageSize == null) {
                return ResultData.ResultDataFaultSelf("分页参数没有传", null);
            }
            //查询条件
            JSONObject condition = json.getJSONObject("condition");
            List<TbDsjRawcheckC642> list = tbDsjRawcheckC642Mapper.selectByPageList2((pageIndex - 1) * pageSize,pageSize * pageIndex,condition);
            Integer dataCount = tbDsjRawcheckC642Mapper.findDataCount2(condition);
            return ResultData.ResultDataSuccess(list,dataCount);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultData.ResultDataFaultSelf("查询失败,失败信息为" + e.getMessage(), null);
        }
    }
    /**
     * 列表导出
     * @param jsonObject
     * @param response
     */
    @Override
    public void export(JSONObject jsonObject, HttpServletResponse response) {
        // 接收传入参数
        Integer pageSize = jsonObject.getInteger("pageSize");
        Integer pageCount = jsonObject.getInteger("pageCount");
        String fileNames = jsonObject.getString("fileName");
        JSONObject condition = jsonObject.getJSONObject("condition");

        // 创建表格数据集合
        List<TbDsjRawcheckC642> listExcel = new ArrayList<>();// 调用service查询出数据
        List<TbDsjRawcheckC642> data = null;

        for (int i = 1; i <= pageCount; i++) {
            // 分页去数据库查询数据 这里可以去数据库查询每一页的数据
            data = tbDsjRawcheckC642Mapper.selectByPageList((i - 1) * pageSize, i * pageSize,condition);
            if (!data.isEmpty() && data.size() > 0) {
                for (TbDsjRawcheckC642 item : data) {
                    listExcel.add(item);
                }
            }
        }
        if (CollectionUtils.isEmpty(listExcel)) {
            return;
        }
        List<ExportTbDsjRawcheckC642> list = new ArrayList<>();
        for (TbDsjRawcheckC642 item : listExcel) {
            ExportTbDsjRawcheckC642 exportTbDsjRawcheckC642 = new ExportTbDsjRawcheckC642();
            BeanUtils.copyProperties(item,exportTbDsjRawcheckC642);
            list.add(exportTbDsjRawcheckC642);
        }
        try {
            Map<String,Object> data1 = new HashMap();
            data1.put("data",list);
            File file = ResourceUtils.getFile("classpath:exceltemplate\\2130连退原料库审核.xlsx");
            TemplateExportParams templateExportParams = new TemplateExportParams(file.getPath(),false);
            templateExportParams.setDictHandler(new PlanExcelHandlerImpl());
            Workbook workbook = ExcelExportUtil.exportExcel(templateExportParams, data1);
            ExcelUtilsPOI.exportExcel(fileNames,response,workbook);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

    }


    /**
     * 列表导出
     * @param jsonObject
     * @param response
     */
    @Override
    public void exportgz(JSONObject jsonObject, HttpServletResponse response) {
        // 接收传入参数
        Integer pageSize = jsonObject.getInteger("pageSize");
        Integer pageCount = jsonObject.getInteger("pageCount");
        String fileNames = jsonObject.getString("fileName");
        JSONObject condition = jsonObject.getJSONObject("condition");

        // 创建表格数据集合
        List<TbDsjRawcheckC642> listExcel = new ArrayList<>();// 调用service查询出数据
        List<TbDsjRawcheckC642> data = null;

        for (int i = 1; i <= pageCount; i++) {
            // 分页去数据库查询数据 这里可以去数据库查询每一页的数据
            data = tbDsjRawcheckC642Mapper.selectByPageList2((i - 1) * pageSize, i * pageSize,condition);
            if (!data.isEmpty() && data.size() > 0) {
                for (TbDsjRawcheckC642 item : data) {
                    listExcel.add(item);
                }
            }
        }
        if (CollectionUtils.isEmpty(listExcel)) {
            return;
        }
        List<ExportTbDsjRawcheckC642> list = new ArrayList<>();
        for (TbDsjRawcheckC642 item : listExcel) {
            ExportTbDsjRawcheckC642 exportTbDsjRawcheckC642 = new ExportTbDsjRawcheckC642();
            BeanUtils.copyProperties(item,exportTbDsjRawcheckC642);
            list.add(exportTbDsjRawcheckC642);
        }
        try {
            Map<String,Object> data1 = new HashMap();
            data1.put("data",list);
            File file = ResourceUtils.getFile("classpath:exceltemplate\\2130连退原料库审核.xlsx");
            TemplateExportParams templateExportParams = new TemplateExportParams(file.getPath(),false);
            templateExportParams.setDictHandler(new PlanExcelHandlerImpl());
            Workbook workbook = ExcelExportUtil.exportExcel(templateExportParams, data1);
            ExcelUtilsPOI.exportExcel(fileNames,response,workbook);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

    }
    @Override
    public ResultData thicknessCCRecalculate(JSONObject jsonObject) {
        // 接收HMI画面输入信息
        JSONObject condition = jsonObject.getJSONObject("condition");
        List<TbDsjRawcheckC642> list = tbDsjRawcheckC642Mapper.thicknessCCRecalculate(condition);
        String dbegin = condition.getString("dbegin");// 开始时间
        String dend = condition.getString("dend");// 结束时间
        String thickdev_1 = condition.getString("thickdev_1");// 厚度公差上限
        String thickdev_2 = condition.getString("thickdev_2");// 厚度公差下限
        String act_thick1 = condition.getString("act_thick1");// 出口厚度上
        String act_thick2 = condition.getString("act_thick2");// 出口厚度下
        String steelgrade = condition.getString("steelgrade");// 内部钢种
        String thickGauge = condition.getString("thickGauge");// 测厚仪
        for (TbDsjRawcheckC642 item : list) {
            // 判断测厚仪是否有值
            if (StringUtils.hasText(thickGauge)) {
                if ("0".equals(thickGauge)) {
                    Query query = new Query(Criteria.where("extid").is(item.getCold_coil_id()));
                    List<TbFalt> faltList = mongoTemplate.find(query, TbFalt.class);
                    saveCC(thickdev_1, thickdev_2, thickGauge, item, faltList, "c622_tcm_exs5_guagedev_thk1");
                } else {
                    Query query = new Query(Criteria.where("extid").is(item.getCold_coil_id()));
                    List<TbFalt> faltList = mongoTemplate.find(query, TbFalt.class);
                    saveCC(thickdev_1, thickdev_2, thickGauge, item, faltList, "c622_tcm_exs5_guagedev_thk2");
                }
            }
        }

        return ResultData.ResultDataFaultSelf("操作成功",1);
    }

    @Override
    public ResultData findByMongDbData(JSONObject jsonObject) {
        String extid = jsonObject.getString("extid");
        if (!StringUtils.hasText(extid)) {
            return ResultData.ResultDataFaultSelf("传入参数有误!",-1);
        }
        Query query = new Query(Criteria.where("extid").is(extid));
        List<RecalculateThkToleC622> recalculateThkToleC622List = mongoTemplate.find(query, RecalculateThkToleC622.class);
        return ResultData.ResultDataFaultSelf("操作成功",recalculateThkToleC622List);
    }

    @Override
    public ResultData findBYCCData(JSONObject jsonObject) {
        Integer pageIndex = jsonObject.getInteger("pageIndex");
        Integer pageSize = jsonObject.getInteger("pageSize");
        if (pageIndex == null || pageSize == null) {
            return ResultData.ResultDataFaultSelf("分页参数没有传", null);
        }
        JSONObject condition = jsonObject.getJSONObject("condition");
        List<TbDsjRecalculateThkToleC622> tbDsjRecalculateThkToleC622s = tbDsjRecalculateThkToleC622mapper.findByData((pageIndex - 1) * pageSize, pageSize * pageIndex, condition);
        Integer count = tbDsjRecalculateThkToleC622mapper.findByDataCount(condition);
        return ResultData.ResultDataSuccess(tbDsjRecalculateThkToleC622s,count);
    }

    @Override
    public ResultData findByWightData(JSONObject jsonObject) {
        JSONObject condition = jsonObject.getJSONObject("condition");
        List<TbDsjActWeight> list = tbDsjActWeightMapper.findByWightData(condition);
        return ResultData.ResultDataSuccess(list);
    }

    @Override
    public ResultData findPageList(JSONObject jsonObject) {
        Integer pageIndex = jsonObject.getInteger("pageIndex");
        Integer pageSize = jsonObject.getInteger("pageSize");
        if (pageIndex == null || pageSize == null) {
            return ResultData.ResultDataFaultSelf("分页参数没有传", null);
        }
        JSONObject condition = jsonObject.getJSONObject("condition");
        try {
            List<TbDsjRawCheck642AndPltcmAscResult> list = tbDsjRawcheckC642Mapper.findPageList((pageIndex - 1) * pageSize, pageSize * pageIndex, condition);
            Integer count = tbDsjRawcheckC642Mapper.findPageListCount(condition);
            return ResultData.ResultDataSuccess(list,count);
        }catch (Exception e){
            e.printStackTrace();
        }
        return ResultData.ResultDataSuccess(null);

    }

    private void saveCC(String thickdev_1, String thickdev_2, String thickGauge, TbDsjRawcheckC642 item, List<TbFalt> faltList,String key) {
        List<Integer> ccIndex = new ArrayList<>(); // 超差位置
        List<Integer> headCcIndex = new ArrayList<>(); // 头部超差位置
        List<Integer> midCcIndex = new ArrayList<>(); // 中部超差位置
        List<Integer> tailCcIndex = new ArrayList<>(); // 尾部超差位置
        for (TbFalt tbFalt : faltList) {
            Map<String, Object> edge = tbFalt.getEdge();
            List<Double> list = (List<Double>) edge.get(key);
            for (int i = 0; i < list.size(); i++) {
                // 超差位置总长度
                // 小于下限 大于上限 为true 存入集合
                if ((list.get(i) < Double.parseDouble(thickdev_1) && list.get(i) > Double.parseDouble(thickdev_2))) {
                    ccIndex.add(i);
                }

                // 超差位置头部
                if (i < 50) {
                    if ((list.get(i) < Double.parseDouble(thickdev_1) && list.get(i) > Double.parseDouble(thickdev_2))) {
                        headCcIndex.add(i);
                    }
                }

                // 超差位置中部
                if (i >= 50 && i < list.size() - 50) {
                    if ((list.get(i) < Double.parseDouble(thickdev_1) && list.get(i) > Double.parseDouble(thickdev_2))) {
                        midCcIndex.add(i);
                    }
                }

                // 超差位置尾部

                if (i >= list.size() - 50) {
                    if ((list.get(i) < Double.parseDouble(thickdev_1) && list.get(i) > Double.parseDouble(thickdev_2))) {
                        tailCcIndex.add(i);
                    }
                }
            }
        }

        TbDsjRecalculateThkToleC622 tbDsjRecalculateThkToleC622 = new TbDsjRecalculateThkToleC622();
        tbDsjRecalculateThkToleC622.setCold_coil_id(item.getCold_coil_id());
        tbDsjRecalculateThkToleC622.setHot_coil_id(item.getHot_coil_id());
        tbDsjRecalculateThkToleC622.setSteelgrade(item.getSteelgrade());
        tbDsjRecalculateThkToleC622.setInner_steelgrade(item.getInner_steelgrade());
        tbDsjRecalculateThkToleC622.setAct_thick(item.getAct_thick());
        tbDsjRecalculateThkToleC622.setAct_width(item.getAct_width());
        tbDsjRecalculateThkToleC622.setAct_len(item.getAct_len());
        tbDsjRecalculateThkToleC622.setAct_outer_dia(item.getAct_outer_dia());
        tbDsjRecalculateThkToleC622.setAct_inner_dia(item.getAct_inner_dia());
        tbDsjRecalculateThkToleC622.setTrim_flag(item.getTrim_flag());
        tbDsjRecalculateThkToleC622.setProd_time(item.getProd_time());
        tbDsjRecalculateThkToleC622.setThk_whole_tole_len(Double.parseDouble(String.valueOf(ccIndex.size())));
        tbDsjRecalculateThkToleC622.setThk_tole_upper(Double.parseDouble(thickdev_1));
        tbDsjRecalculateThkToleC622.setThk_tole_lower(Double.parseDouble(thickdev_2));
        tbDsjRecalculateThkToleC622.setCrew(item.getCrew());
        tbDsjRecalculateThkToleC622.setShift(item.getShift());
        tbDsjRecalculateThkToleC622.setGauge_flag(Double.parseDouble(thickGauge));
        tbDsjRecalculateThkToleC622.setThk_head_tole_len(Double.parseDouble(String.valueOf(headCcIndex.size())));
        tbDsjRecalculateThkToleC622.setThk_mid_tole_len(Double.parseDouble(String.valueOf(midCcIndex.size())));
        tbDsjRecalculateThkToleC622.setThk_tail_tole_len(Double.parseDouble(String.valueOf(tailCcIndex.size())));
        tbDsjRecalculateThkToleC622mapper.insert(tbDsjRecalculateThkToleC622);

        RecalculateThkToleC622 recalculateThkToleC622 = new RecalculateThkToleC622();
        recalculateThkToleC622.setExtid(item.getCold_coil_id());
        recalculateThkToleC622.setThk_tole(ccIndex);
        mongoTemplate.save(recalculateThkToleC622);
    }

    @Override
    public ResultData findLineDataByVolumeNo(JSONObject json) {
        String cold_coil_id = json.getString("cold_coil_id");
        String hot_coil_id = json.getString("hot_coil_id");
        if (!StringUtils.hasText(cold_coil_id) || !StringUtils.hasText(hot_coil_id)) {
            return ResultData.ResultDataFaultSelf(null, "传入热卷号或者冷卷号为空");
        }

        Query query1 = new Query(Criteria.where("volumeNo").is(hot_coil_id));
        Query query2 = new Query(Criteria.where("extid").is(cold_coil_id));

        TbFalt jDataSave622 = mongoTemplate.findOne(query2, TbFalt.class);
        QmsCurveDataMongo qmsCurveDataMongo = mongoTemplate.findOne(query1, QmsCurveDataMongo.class);

        Map<String, Map<String, Object>> map = new HashMap<>();
        Map<String, Object> collect = new HashMap<>();
        if(!ObjectUtils.isEmpty(jDataSave622)){
            Object c622_tcm_exs5_thk1 = jDataSave622.getEdge().get("c622_tcm_exs5_thk1");
            Object c622_tcm_exs5_thk2 = jDataSave622.getEdge().get("c622_tcm_exs5_thk2");
            Object c622_tcm_exs5_guagedev_thk2 = jDataSave622.getEdge().get("c622_tcm_exs5_guagedev_thk2");
            // Object c622_tcm_exs4_shape_width_act = jDataSave622.getEdge().get("c622_tcm_exs4_shape_width_act");
            Object c622_tcm_exs5_speed = jDataSave622.getEdge().get("c622_tcm_exs5_speed");
            Object c622_tcm_shape_channel1 = jDataSave622.getEdge().get("c622_tcm_shape_channel1");
            Object c622_tcm_shape_channel2 = jDataSave622.getEdge().get("c622_tcm_shape_channel2");
            Object c622_tcm_shape_channel3 = jDataSave622.getEdge().get("c622_tcm_shape_channel3");
            Object c622_tcm_shape_channel4 = jDataSave622.getEdge().get("c622_tcm_shape_channel4");
            Object c622_tcm_shape_channel5 = jDataSave622.getEdge().get("c622_tcm_shape_channel5");
            Object c622_tcm_shape_channel6 = jDataSave622.getEdge().get("c622_tcm_shape_channel6");
            Object c622_tcm_shape_channel7 = jDataSave622.getEdge().get("c622_tcm_shape_channel7");
            Object c622_tcm_shape_channel8 = jDataSave622.getEdge().get("c622_tcm_shape_channel8");
            Object c622_tcm_shape_channel9 = jDataSave622.getEdge().get("c622_tcm_shape_channel9");
            Object c622_tcm_shape_channel10 = jDataSave622.getEdge().get("c622_tcm_shape_channel10");
            Object c622_tcm_shape_channel11 = jDataSave622.getEdge().get("c622_tcm_shape_channel11");
            Object c622_tcm_shape_channel12 = jDataSave622.getEdge().get("c622_tcm_shape_channel12");
            Object c622_tcm_shape_channel13 = jDataSave622.getEdge().get("c622_tcm_shape_channel13");
            Object c622_tcm_shape_channel14 = jDataSave622.getEdge().get("c622_tcm_shape_channel14");
            Object c622_tcm_shape_channel15 = jDataSave622.getEdge().get("c622_tcm_shape_channel15");
            Object c622_tcm_shape_channel16 = jDataSave622.getEdge().get("c622_tcm_shape_channel16");
            Object c622_tcm_shape_channel17 = jDataSave622.getEdge().get("c622_tcm_shape_channel17");
            Object c622_tcm_shape_channel18 = jDataSave622.getEdge().get("c622_tcm_shape_channel18");
            Object c622_tcm_shape_channel19 = jDataSave622.getEdge().get("c622_tcm_shape_channel19");
            Object c622_tcm_shape_channel20 = jDataSave622.getEdge().get("c622_tcm_shape_channel20");
            Object c622_tcm_shape_channel21 = jDataSave622.getEdge().get("c622_tcm_shape_channel21");
            Object c622_tcm_shape_channel22 = jDataSave622.getEdge().get("c622_tcm_shape_channel22");
            Object c622_tcm_shape_channel23 = jDataSave622.getEdge().get("c622_tcm_shape_channel23");
            Object c622_tcm_shape_channel24 = jDataSave622.getEdge().get("c622_tcm_shape_channel24");
            Object c622_tcm_shape_channel25 = jDataSave622.getEdge().get("c622_tcm_shape_channel25");
            Object c622_tcm_shape_channel26 = jDataSave622.getEdge().get("c622_tcm_shape_channel26");
            Object c622_tcm_shape_channel27 = jDataSave622.getEdge().get("c622_tcm_shape_channel27");
            Object c622_tcm_shape_channel28 = jDataSave622.getEdge().get("c622_tcm_shape_channel28");
            Object c622_tcm_shape_channel29 = jDataSave622.getEdge().get("c622_tcm_shape_channel29");
            Object c622_tcm_shape_channel30 = jDataSave622.getEdge().get("c622_tcm_shape_channel30");
            Object c622_tcm_shape_channel31 = jDataSave622.getEdge().get("c622_tcm_shape_channel31");
            Object c622_tcm_shape_channel32 = jDataSave622.getEdge().get("c622_tcm_shape_channel32");
            Object c622_tcm_shape_channel33 = jDataSave622.getEdge().get("c622_tcm_shape_channel33");
            Object c622_tcm_shape_channel34 = jDataSave622.getEdge().get("c622_tcm_shape_channel34");
            Object c622_tcm_shape_channel35 = jDataSave622.getEdge().get("c622_tcm_shape_channel35");
            Object c622_tcm_shape_channel36 = jDataSave622.getEdge().get("c622_tcm_shape_channel36");
            Object c622_tcm_shape_channel37 = jDataSave622.getEdge().get("c622_tcm_shape_channel37");
            Object c622_tcm_shape_channel38 = jDataSave622.getEdge().get("c622_tcm_shape_channel38");
            Object c622_tcm_shape_channel39 = jDataSave622.getEdge().get("c622_tcm_shape_channel39");
            Object c622_tcm_shape_channel40 = jDataSave622.getEdge().get("c622_tcm_shape_channel40");
            collect.put("c622_tcm_exs5_thk1", c622_tcm_exs5_thk1);
            collect.put("c622_tcm_exs5_thk2", c622_tcm_exs5_thk2);
            collect.put("c622_tcm_exs5_guagedev_thk2", c622_tcm_exs5_guagedev_thk2);
            // collect.put("c622_tcm_exs4_shape_width_act", c622_tcm_exs4_shape_width_act);
            collect.put("c622_tcm_exs5_speed", c622_tcm_exs5_speed);
            collect.put("c622_tcm_shape_channel1", c622_tcm_shape_channel1);
            collect.put("c622_tcm_shape_channel2", c622_tcm_shape_channel2);
            collect.put("c622_tcm_shape_channel3", c622_tcm_shape_channel3);
            collect.put("c622_tcm_shape_channel4", c622_tcm_shape_channel4);
            collect.put("c622_tcm_shape_channel5", c622_tcm_shape_channel5);
            collect.put("c622_tcm_shape_channel6", c622_tcm_shape_channel6);
            collect.put("c622_tcm_shape_channel7", c622_tcm_shape_channel7);
            collect.put("c622_tcm_shape_channel8", c622_tcm_shape_channel8);
            collect.put("c622_tcm_shape_channel9", c622_tcm_shape_channel9);
            collect.put("c622_tcm_shape_channel10", c622_tcm_shape_channel10);
            collect.put("c622_tcm_shape_channel11", c622_tcm_shape_channel11);
            collect.put("c622_tcm_shape_channel12", c622_tcm_shape_channel12);
            collect.put("c622_tcm_shape_channel13", c622_tcm_shape_channel13);
            collect.put("c622_tcm_shape_channel14", c622_tcm_shape_channel14);
            collect.put("c622_tcm_shape_channel15", c622_tcm_shape_channel15);
            collect.put("c622_tcm_shape_channel16", c622_tcm_shape_channel16);
            collect.put("c622_tcm_shape_channel17", c622_tcm_shape_channel17);
            collect.put("c622_tcm_shape_channel18", c622_tcm_shape_channel18);
            collect.put("c622_tcm_shape_channel19", c622_tcm_shape_channel19);
            collect.put("c622_tcm_shape_channel20", c622_tcm_shape_channel20);
            collect.put("c622_tcm_shape_channel21", c622_tcm_shape_channel21);
            collect.put("c622_tcm_shape_channel22", c622_tcm_shape_channel22);
            collect.put("c622_tcm_shape_channel23", c622_tcm_shape_channel23);
            collect.put("c622_tcm_shape_channel24", c622_tcm_shape_channel24);
            collect.put("c622_tcm_shape_channel25", c622_tcm_shape_channel25);
            collect.put("c622_tcm_shape_channel26", c622_tcm_shape_channel26);
            collect.put("c622_tcm_shape_channel27", c622_tcm_shape_channel27);
            collect.put("c622_tcm_shape_channel28", c622_tcm_shape_channel28);
            collect.put("c622_tcm_shape_channel29", c622_tcm_shape_channel29);
            collect.put("c622_tcm_shape_channel30", c622_tcm_shape_channel30);
            collect.put("c622_tcm_shape_channel31", c622_tcm_shape_channel31);
            collect.put("c622_tcm_shape_channel32", c622_tcm_shape_channel32);
            collect.put("c622_tcm_shape_channel33", c622_tcm_shape_channel33);
            collect.put("c622_tcm_shape_channel34", c622_tcm_shape_channel34);
            collect.put("c622_tcm_shape_channel35", c622_tcm_shape_channel35);
            collect.put("c622_tcm_shape_channel36", c622_tcm_shape_channel36);
            collect.put("c622_tcm_shape_channel37", c622_tcm_shape_channel37);
            collect.put("c622_tcm_shape_channel38", c622_tcm_shape_channel38);
            collect.put("c622_tcm_shape_channel39", c622_tcm_shape_channel39);
            collect.put("c622_tcm_shape_channel40", c622_tcm_shape_channel40);
        }
        map.put("collect", collect);

        Map<String, Object> blob = new HashMap<>();
        if(!ObjectUtils.isEmpty(qmsCurveDataMongo)){
            if (qmsCurveDataMongo.getOriginBolg() == null) {
                map.put("blob", null);
            } else {
                List<Double> list3 = qmsCurveDataMongo.getOriginBolg().get("hsm_crown_c40");
                List<Double> list4 = qmsCurveDataMongo.getOriginBolg().get("hsm_flatness_wedge40");
                List<Double> list5 = qmsCurveDataMongo.getOriginBolg().get("hsm_fdt_fdt");
                List<Double> list6 = qmsCurveDataMongo.getOriginBolg().get("hsm_ctc_ct_temp");
                List<Double> list7 = qmsCurveDataMongo.getOriginBolg().get("hsm_flatness_symmetric_flatness");
                List<Double> list8 = qmsCurveDataMongo.getOriginBolg().get("hsm_flatness_asymmetric_flatness");
                List<Double> list9 = qmsCurveDataMongo.getOriginBolg().get("hsm_fdw_width_cold");

                blob.put("hsm_crown_c40", list3);
                blob.put("hsm_flatness_wedge40", list4);
                blob.put("hsm_fdt_fdt", list5);
                blob.put("hsm_ctc_ct_temp", list6);
                blob.put("hsm_flatness_symmetric_flatness", list7);
                blob.put("hsm_flatness_asymmetric_flatness", list8);
                blob.put("hsm_fdw_width_cold", list9);
            }
        }
        map.put("blob", blob);

        if (!CollectionUtils.isEmpty(map)) {
            return ResultData.ResultDataSuccess(map);
        } else {
            return ResultData.ResultDataFaultSelf("查询数据为空",null);
        }
    }

    @Override
    public void updateWeight(String extid, String weight,String code) {

        Double num = 0.0;
        ComparisonExtid comparisonExtid = null;
        WeightMessage weightMessage = new WeightMessage();
        if (code.equals("C621")) {
            comparisonExtid = comparisonExtidC621Mapper.comparisonExtidC621(extid);
            if (comparisonExtid != null) {
                if (comparisonExtid.getEntry_weight() != 0) {
                    num = Double.parseDouble(weight) / comparisonExtid.getEntry_weight() * 100;
                }
            }
            weightMessage.setMsgId(1050);
            weightMessage.setExtid(extid);
            weightMessage.setMsgIdName("Evaluate");
        } else if (code.equals("C622")) {
            comparisonExtid = comparisonExtidC622Mapper.comparisonExtidC622(extid);
            if (comparisonExtid != null) {
                if (comparisonExtid.getEntry_weight() != 0) {
                    num = Double.parseDouble(weight) / comparisonExtid.getEntry_weight() * 100;
                }
            }
            weightMessage.setMsgId(1051);
            weightMessage.setExtid(extid);
            weightMessage.setMsgIdName("CalRaw");
        } else if (code.equals("C642")) {
            comparisonExtid = comparisonExtidC642Mapper.comparisonExtidC642(extid);
            if (comparisonExtid != null) {
                if (comparisonExtid.getEntry_weight() != 0) {
                    num = Double.parseDouble(weight) / comparisonExtid.getEntry_weight() * 100;
                }
            }
//            weightMessage.setMsgId(1052);
//            weightMessage.setExtid(extid);
//            weightMessage.setMsgIdName("CalRaw");
        }
        // 构建条件构造器
        LambdaQueryWrapper<TbDsjActWeight> queryWrapper = new LambdaQueryWrapper();
        queryWrapper.eq(TbDsjActWeight::getExtid,extid);
        TbDsjActWeight tbDsjActWeight1 = tbDsjActWeightMapper.selectOne(queryWrapper);
        if (tbDsjActWeight1 != null) {
            return;
        }
        TbDsjActWeight tbDsjActWeight = new TbDsjActWeight();
        tbDsjActWeight.setLinecode(code);
        tbDsjActWeight.setAct_wt(Double.parseDouble(weight));
        tbDsjActWeight.setExtid(extid);
        tbDsjActWeight.setYield(num);

        if (comparisonExtid != null) {
            tbDsjActWeight.setCust_mat_no(comparisonExtid.getEntid());
            tbDsjActWeight.setIn_mat_wt(comparisonExtid.getEntry_weight());
            tbDsjActWeight.setEx_thk(comparisonExtid.getDeliver_thick());
            tbDsjActWeight.setEx_len(comparisonExtid.getDeliver_length());
            tbDsjActWeight.setEn_len(comparisonExtid.getEntry_length());
            tbDsjActWeight.setEn_thk(comparisonExtid.getEntry_thick());
            tbDsjActWeight.setEn_wid(comparisonExtid.getHwid());
            tbDsjActWeight.setSteel_grade(comparisonExtid.getSteel_grade());
            tbDsjActWeight.setTrim_flag(comparisonExtid.getTrim());
            tbDsjActWeight.setCrew(comparisonExtid.getCrew());
            tbDsjActWeight.setShift(comparisonExtid.getShift());
            if (code.equals("C621") && (comparisonExtid.getDeliver_width_act() - comparisonExtid.getWidth_max()) > 5) {
                tbDsjActWeight.setEx_wid(comparisonExtid.getWidth_max());
            } else {
                tbDsjActWeight.setEx_wid(comparisonExtid.getDeliver_width_act());
            }
            Timestamp process_start_time = comparisonExtid.getProcess_start_time();
            Timestamp process_end_time = comparisonExtid.getProcess_end_time();
            long startTime = process_start_time.getTime();
            long endTime = process_end_time.getTime();
            long duration = endTime - startTime;
            tbDsjActWeight.setDuration(Double.parseDouble(Math.ceil(duration / 1000)  + ""));

            tbDsjActWeight.setProcess_start_time(comparisonExtid.getProcess_start_time());
            tbDsjActWeight.setProcess_end_time(comparisonExtid.getProcess_end_time());
            tbDsjActWeight.setNext_unit_no(comparisonExtid.getSucceed_process());
        }
        tbDsjActWeightMapper.insert(tbDsjActWeight);


        LambdaQueryWrapper<TbDsjActYield> queryWrapper1 = new LambdaQueryWrapper<>();
        queryWrapper1.eq(TbDsjActYield::getCust_mat_no,comparisonExtid.getEntid());

        List<TbDsjActYield> tbDsjActYields = tbDsjActYieldMapper.selectList(queryWrapper1);

        if (tbDsjActYields.size() != 0) {
            TbDsjActYield tbDsjActYield = new TbDsjActYield();
            for (TbDsjActYield item : tbDsjActYields) {
                tbDsjActYield.setYield(tbDsjActWeight.getYield() + item.getYield());
                tbDsjActYield.setAct_wt(tbDsjActWeight.getAct_wt() + item.getAct_wt());
                tbDsjActYield.setExtid(tbDsjActWeight.getExtid());
                tbDsjActYield.setLinecode(tbDsjActWeight.getLinecode());
                tbDsjActYield.setCust_mat_no(tbDsjActWeight.getCust_mat_no());
                tbDsjActYield.setIn_mat_wt(tbDsjActWeight.getIn_mat_wt());
                tbDsjActYield.setProcess_end_time(tbDsjActWeight.getProcess_end_time());
                tbDsjActYield.setCrew(tbDsjActWeight.getCrew());
                tbDsjActYield.setShift(tbDsjActWeight.getShift());
                tbDsjActYield.setTrim_flag(Double.parseDouble(tbDsjActWeight.getTrim_flag()));
                tbDsjActYield.setNext_unit_no(tbDsjActWeight.getNext_unit_no());
            }
            tbDsjActYieldMapper.update(tbDsjActYield,queryWrapper1);
        } else {
            TbDsjActYield tbDsjActYield = new TbDsjActYield();
            tbDsjActYield.setYield(tbDsjActWeight.getYield());
            tbDsjActYield.setAct_wt(tbDsjActWeight.getAct_wt());
            tbDsjActYield.setExtid(tbDsjActWeight.getExtid());
            tbDsjActYield.setLinecode(tbDsjActWeight.getLinecode());
            tbDsjActYield.setCust_mat_no(tbDsjActWeight.getCust_mat_no());
            tbDsjActYield.setIn_mat_wt(tbDsjActWeight.getIn_mat_wt());
            tbDsjActYield.setProcess_end_time(tbDsjActWeight.getProcess_end_time());
            tbDsjActYield.setCrew(tbDsjActWeight.getCrew());
            tbDsjActYield.setShift(tbDsjActWeight.getShift());
            tbDsjActYield.setTrim_flag(Double.parseDouble(tbDsjActWeight.getTrim_flag()));
            tbDsjActYield.setNext_unit_no(tbDsjActWeight.getNext_unit_no());
            tbDsjActYieldMapper.insert(tbDsjActYield);
        }
        log.error("发送消息");
        this.sendmessage(weightMessage);
    }

    private void sendmessage(WeightMessage weightMessage) {
        String str = "{\n" +
                " \"action\": \"send_msg\",\n" +
                " \"body\": {\n" +
                "  \"msgId\": 2024,\n" +
                "  \"params\": [\n" +
                "   [\n" +
                "    {\n" +
                "        \"volumeNo\": \"X22B56596800001\"\n" +
                "    }\n" +
                "   ]\n" +
                "  ],\n" +
                "  \"sendTo\": \"Cal_Energy\",\n" +
                "  \"timeout\": 2,\n" +
                "  \"waitReply\": true\n" +
                " },\n" +
                " \"operator\": \"director\",\n" +
                " \"terminal\": \"director\"\n" +
                "}";
        JSONObject parseObject = null;
        parseObject = JSON.parseObject(str);
        Set<String> keySet = parseObject.keySet();
        for (String item : keySet) {
            Object obj = parseObject.get(item);
            if (obj instanceof JSONObject) {
                ((JSONObject) obj).put("msgId", weightMessage.getMsgId());
                ((JSONObject) obj).put("sendTo",weightMessage.getMsgIdName());
                if (((JSONObject) obj).get("params") instanceof JSONArray) {
                    JSONArray jsonArray = (JSONArray) ((JSONObject) obj).get("params");
                    for (int i = 0; i < jsonArray.size(); i++) {
                        Object o = jsonArray.get(i);
                        for (Object o1 : (JSONArray) o) {
                            ((JSONObject) o1).put("volumeNo", weightMessage.getExtid());
                        }
                    }
                }
            }
        }
        log.error(parseObject.toString());
        restTemplate.postForObject("http://170.0.200.112:8088/HMI", parseObject, String.class);
    }



}




